<?php
/**
 * STEP 3
 */

include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/function.php');

$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(10);

//将下载地址压入队列
$lsize = $redis->lSize('QISUU_DOWN_LINK');
echo2($lsize . ' in queue', 'QISUU_DOWN');

$count = $old = $post = 0;
$totalsize = 0;
if($lsize < 100) {
    $files = scandir(QISUU_NEW . 'data/');
    foreach ($files as $file) {
        if(!stristr($file, '.html')) {
            continue;
        }

        $contents = file_get_contents(QISUU_NEW . 'data/'.$file);
        $contents = json_decode($contents, true);

        if($contents['info']) {
            $size  = $contents['info'][2];
            $fsize = floatval($size);

            if(stristr($size, 'mb')) {
                $totalsize += $fsize * 1024;
            }
            elseif(stristr($size, 'kb')) {
                $totalsize += $fsize;
            }
        }

        //检查是否已下载
        $url = $contents['download'][0];
        $basename = basename($url);
        $basename_arr = explode('.', $basename);
        $ext = array_pop($basename_arr);
        if(!empty($basename) && (empty($ext) || $ext == 'ar' || ($basename == $ext && $ext != $url))) {
            $ext = 'rar';
        }
        if($ext != 'rar' && $ext != 'txt' && $ext != 'zip') {
            $redis->hSet('QISUU_CRASH_DATA', './data/'.$file, 1);
            //echo2($basename . ' ' . $ext . ' ' . $url); 
            continue;
        }
        if($redis->hExists('QISUU_CRASH_DOWN', $url)) {
            //echo2($url . ' in Crash');
            continue;
        }

        $savename_old = $contents['title']. '.'.$ext;
        $savename = str_ireplace(['(',')',':','*','?','/'], '', $savename_old);
        $savename = QISUU_NEW . 'download/' . $contents['classname'] . '/' . $contents['typename'] . '/' . $savename;
        $oldname = QISUU_NEW . 'download/' . $contents['classname'] . '/' . $contents['typename'] . '/' . $savename_old;

        if(file_exists($savename) && filesize($savename) < 100) {
            unlink($savename);
        }
        if(file_exists($oldname) && filesize($oldname) < 100) {
            unlink($oldname);
        }
        
        if(file_exists($savename) || file_exists($oldname)) {
            $old++;
            echo2($old, 'old');
            continue;
        }

        if($contents['download']) {
            $contents['savename'] = $savename;
            $redis->lPush('QISUU_DOWN_LINK', json_encode($contents));
            $count++;
        }
        else {
            $redis->hSet('QISUU_CRASH_DATA', './data/'.$file, 1);
        }
    }
}

$redis->lPush('stats_qisuu_down', "[".date('Y-m-d H:i:s')."], down_old:$old, down_queue_new:$count");
echo2($count . ' push queue, old ' . $old, 'QISUU_DOWN');
echo2($totalsize . ' KB', 'QISUU_DOWN');

//从队列拿下载地址下载文件
while (true) {
    $stop = $redis->get('QISUU_QUEUE_STOP');
    if($stop) {
        echo2('stop', 'QISUU_DOWN'); break;
    }

    $data = $redis->lPop('QISUU_DOWN_LINK');

    if(!$data) {
        echo2('empty', 'QISUU_DOWN'); break;
    }

    $data = json_decode($data, true);
    $url = $data['download'][0];
    $basename = basename($url);
    $basename_arr = explode('.', $basename);
    $ext = array_pop($basename_arr);

    if(!empty($basename) && (empty($ext) || $ext == 'ar' || ($basename == $ext && $ext != $url))) {
        $url .= '.rar';
        $ext = 'rar';
    }
    if($ext != 'rar' && $ext != 'txt' && $ext != 'zip') {
        echo2($basename, 'QISUU_DOWN'); 
        continue;
    }
    if($redis->hExists('QISUU_CRASH_DOWN', $url)) {
        echo2($url . ' in Crash', 'QISUU_DOWN');
        continue;
    }

    //建立多级目录
    $dir = QISUU_NEW . 'download/' . $data['classname'] . '/' . $data['typename'];
    shell_exec("mkdir -p {$dir}");

    $savename_old = $data['title']. '.'.$ext;
    $savename = str_ireplace(['(',')',':','*','?','/'], '', $savename_old);

    $pathname = QISUU_NEW . 'download/' . $data['classname'];
    if(!file_exists($pathname)) {
        mkdir($pathname);
    }

    $pathname .= '/' . $data['typename'];
    if(!file_exists($pathname)) {
        mkdir($pathname);
    }

    $savename = QISUU_NEW . 'download/' . $data['classname'] . '/' . $data['typename'] . '/' . $savename;
    $oldname = QISUU_NEW . 'download/' . $data['classname'] . '/' . $data['typename'] . '/' . $savename_old;
    if($savename != $oldname && file_exists($oldname)) {
        rename($oldname, $savename);
        echo2($oldname . ' mv to ' . $savename, 'QISUU_DOWN');
        var_dump($savename != $oldname);
        continue;
    }

    if(file_exists($savename)) {
        echo2($savename . ' file exists', 'QISUU_DOWN');
        continue;
    }

    $shell = "wget -t 5 -c --restrict-file-names=nocontrol -O '{$savename}' '".$url."'";
    echo2($shell, 'QISUU_DOWN');
    $result = shell_exec($shell);
    //echo2($result);
    
    if(file_exists($savename) && filesize($savename) > 100) {
        //$redis->lPush('QISUU_POST_LINK', json_encode($data));
        $post++;
    }
    else {
        unlink($savename);
        $redis->hSet('QISUU_CRASH_DOWN', $url, 1);
    }

    //break;
}

$redis->lPush('stats_qisuu_down', "[".date('Y-m-d H:i:s')."],down_success_new:$post");
echo2($post . ' post queue', 'QISUU_DOWN');

sleep(10);

echo2('sleep exit', 'QISUU_DOWN');
